home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 958 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: news.ahc.ameritech.com!datalytics!news
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating a pointer to a function "void (*ptrFunction)()"  inside a class
  5. Date: 8 Jan 1996 17:35:42 GMT
  6. Organization: Datalytics, Inc
  7. Message-ID: <4crkle$h4r@gold.datalytics.com>
  8. References: <30ECA10F.3D99@ifu.net>
  9. NNTP-Posting-Host: pc071.datalytics.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. Jason Gresh <gresh@ifu.net> wrote:
  16. >    I am trying to create a base class that has a function that the 
  17. >derived class needs to create.  In order to do this, I want to create a 
  18. >pointer to a function in the base class that the derived class can then 
  19. >define.  This is not a case where an override will work because the 
  20. >number and names of the derived functions is not known.  >
  21. >class BaseClass
  22. >{
  23. >    int (*ptrFunction)();
  24. >}
  25. >class DerivedClass : BaseClass
  26. >{
  27. >    DerivedClass::DerivedClass();
  28. >    int myFunction(int, int);
  29. >}
  30. >DerivedClass::DerivedClass()
  31. >{
  32. >    ptrFunction = myFunction;
  33. >}
  34. >DerivedClass::myFunction(int, int)
  35. >{
  36. >// code ....
  37. >}
  38. >If I don't make myFunction part of the derived class (global), 
  39. >everything works fine.  As soon as I include it in the class, I cannot 
  40. >assign a pointer to it.  I am aware that pointers to functions inside 
  41. >the class include the class name in some way ( this is not an issue for 
  42. >global functions).  What is the casting (or other) mechanism to make
  43. >this work?
  44. >
  45. This behavior will only work for static member functions.  By 
  46. declaring a member function static, you are asking the 
  47. compiler to avoid mangling the name.
  48.  
  49. On the other hand, by making a member function static, you 
  50. lose the this pointer in the function call and the member 
  51. function can no longer operate on an instance of the class.  
  52. If you wish to restore that capability, you must pass a this 
  53. pointer explicitly.  That is, the function can take a pointer 
  54. to the appropriate class parameter.
  55.  
  56. If that "this" pointer is to be of a specific type (to match 
  57. the member function's derived type, you'll need to use RTTI to 
  58. validate that the "this" pointer is of the correct type.  (The 
  59. function pointer type will necessarily take a pointer to the 
  60. base class, as must the derived class member functions.  If 
  61. you need a derived class pointer, you need to determine if a 
  62. cast to the desired derived type is valid.  RTTI is the only 
  63. way to do that--whether you or the compiler implements RTTI is 
  64. moot.)
  65.  
  66. -- 
  67. Robert Stewart        | My opinions are usually my own.
  68. Datalytics, Inc.
  69. (513)226-7700
  70. stew@datalytics.com
  71.  
  72.  
  73.